package org.design;

import java.util.ArrayList;
import java.util.List;

import org.adapter.MerchantAdapter;
import org.cmn.MerchantPojo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class Merchants extends Activity {

	private ListView list;
	private EditText searchMerchant;
	private int textlength = 0;
	private ArrayList<MerchantPojo> array_sort = new ArrayList<MerchantPojo>();
	private List<MerchantPojo> listOfMerchants;
	public static MerchantAdapter searchAdapter;
	public static MerchantAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.merchants);

		list = (ListView) findViewById(R.id.allMerchants);
		list.setClickable(true);
		registerForContextMenu(list);

		searchMerchant = (EditText) findViewById(R.id.merchantSearch);
		searchMerchant.setCompoundDrawablesWithIntrinsicBounds(0, 0,
				android.R.drawable.ic_menu_search, 0);

		listOfMerchants = new ArrayList<MerchantPojo>();
		listOfMerchants.add(new MerchantPojo("Saler 1", "+359 884 450279"));
		listOfMerchants.add(new MerchantPojo("Saler 2", "+359 884 213869"));
		listOfMerchants.add(new MerchantPojo("Saler 3", "+359 884 023411"));
		listOfMerchants.add(new MerchantPojo("Saler 4", "+359 884 561237"));
		listOfMerchants.add(new MerchantPojo("Saler 5", "+359 884 654466"));
		listOfMerchants.add(new MerchantPojo("Saler 6", "+359 884 984565"));
		listOfMerchants.add(new MerchantPojo("Saler 7", "+359 884 327985"));
		listOfMerchants.add(new MerchantPojo("Saler 8", "+359 884 465465"));
		listOfMerchants.add(new MerchantPojo("Saler 9", "+359 882 464325"));
		listOfMerchants.add(new MerchantPojo("Saler 10", "+359 884 235234"));

		adapter = new MerchantAdapter(this, listOfMerchants);

		searchAdapter = new MerchantAdapter(this, array_sort);

		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long index) {
				Intent merchantIntent = new Intent(view.getContext(),
						Sales.class);
				view.getContext().startActivity(merchantIntent);

				System.out.println("sadsfsf");
				// showToast(listOfMerchants.get(position).getName());
			}
		});

		list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
				menu.setHeaderTitle("Saler Actions");
				menu.add(0, info.position, 0, "Edit");
				menu.add(0, info.position, 0, "Delete");

			}
		});

		searchMerchant.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
				// Abstract Method of TextWatcher Interface.
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				System.out.println("");
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// if (searchMerchant.getText().toString()
				// .indexOf(R.string.searchKeyWord) > -1) {
				// searchMerchant.getText().clear();
				// }

				textlength = searchMerchant.getText().length();
				array_sort.clear();
				for (int i = 0; i < listOfMerchants.size(); i++) {
					if (textlength <= listOfMerchants.get(i).getName().length()) {
						if (searchMerchant
								.getText()
								.toString()
								.equalsIgnoreCase(
										(String) listOfMerchants.get(i)
												.getName()
												.subSequence(0, textlength))) {
							array_sort.add(listOfMerchants.get(i));
						}
					}
				}
				list.setAdapter(Merchants.searchAdapter);
			}
		});

		list.setAdapter(adapter);

		ImageView addImage = (ImageView) findViewById(R.id.addMerchant);
		addImage.setClickable(true);
		addImage.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent merchantIntent = new Intent(v.getContext(),
						NewMerchant.class);
				//merchantIntent.putExtra(ActivityConsts.TEST, new MerchantPojo());
				v.getContext().startActivity(merchantIntent);
			}
		});

		ImageView addMerchant = (ImageView) findViewById(R.id.addMerchant);
		addMerchant.setImageResource(android.R.drawable.ic_input_add);

	}
	
	private void initView(){
		// init controls
		
		// set actions and listeners
	}

	private void showToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}

	public MerchantAdapter getSearchAdapter() {
		return searchAdapter;
	}

	public static Merchants createInstance() {
		return new Merchants();
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int position = -1;

		if (item.getTitle().toString().equals("Edit")) {
			position = item.getItemId();
		} else if (item.getTitle().toString().equals("Delete")) {
			position = item.getItemId();
		}

		return false;
	}

}
